﻿#include <SDL/SDL_image.h>
#include "constantes.h"
#include "SelectModeScreen.h"
#include "NumberGeneratorScreen.h"

using namespace SDL::UTILS;

SelectModeArgs::SelectModeArgs(SDL_Surface* _screen, SDL_Event* _event) : MyScreenArgs(_screen, _event)
{
}

SelectModeArgs::~SelectModeArgs()
{
}



SelectModeScreen::SelectModeScreen() : IScreen(),
	m_pbGenerator(L"Générateur")
{
	m_currentScreen.Screen = NULL;
	m_currentScreen.Args = NULL;
	m_surface.SetSurface(IMG_Load(std::string(CST::BG_PATH + "selectmode.png").c_str()));
	this->InitButtons();
}

SelectModeScreen::~SelectModeScreen()
{
	m_screens.Clear();
}

void SelectModeScreen::Draw(IArgs* _args)
{
	SelectModeArgs* sma = dynamic_cast<SelectModeArgs*>(_args);

	if (m_currentScreen.Screen != NULL)
	{
		m_screens.Draw(NULL);
	}
	else
	{
		m_pbGenerator.Draw(m_surface.GetSurface());
		m_surface.BlitSurface(NULL, sma->Screen, NULL);
	}
}

void SelectModeScreen::Exec(IArgs* _args)
{
	SelectModeArgs* sma = dynamic_cast<SelectModeArgs*>(_args);

	if (m_currentScreen.Screen != NULL)
	{
		m_screens.Exec(NULL);
	}
	else
	{
		m_pbGenerator.UpdateState();

		if (sma->Event->type == SDL_KEYDOWN)
		{
			if (sma->Event->key.keysym.sym == SDLK_ESCAPE)
				sma->Value = 1;
		}
	}
}

bool SelectModeScreen::Manage(SDL::UTILS::ScreenManager::Element* _caller, SDL::UTILS::IArgs* _args)
{
	SelectModeScreen* sms = dynamic_cast<SelectModeScreen*>(_caller->Object);

	if (sms->m_currentScreen.Screen != NULL)
	{
		sms->m_screens.Manage(NULL);
		if (sms->m_currentScreen.Args->Value != 0)
		{
			sms->m_screens.Clear();
			sms->m_currentScreen.Screen = NULL;
			sms->m_currentScreen.Args = NULL;
		}
	}
	else
	{
		if (sms->m_pbGenerator.GetState() == SDL::BUTTON::STATE::CLICKED)
			sms->On_pbGenerator_Clicked(_caller->Datas);
	}
	return (true);
}

void SelectModeScreen::InitButtons()
{
	Rect upperRight(0, 0, m_surface.GetW(), m_surface.GetH());
	Sint16 xbutton = upperRight.x + (upperRight.w / 2 - m_pbGenerator.GetDefaultSize().w / 2);
	Sint16 ybutton = 300;
	
	m_pbGenerator.SetPosition(xbutton, ybutton);
	ybutton += 48;
}

void SelectModeScreen::On_pbGenerator_Clicked(SDL::UTILS::IArgs* _datas)
{
	SelectModeArgs* sma = dynamic_cast<SelectModeArgs*>(_datas);

	m_screens.Clear();

	m_currentScreen.Screen = new NumberGeneratorScreen();
	m_currentScreen.Args = new NumberGeneratorArgs(sma->Screen, sma->Event);
	m_screens.Add("generator", m_currentScreen.Screen, NumberGeneratorScreen::Manage, m_currentScreen.Args);
	
	m_pbGenerator.SetState(SDL::BUTTON::RELEASED);
}
